package com.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.user.entity.Users;
import com.user.mapper.UsersMapper;
import com.user.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cd
 * @version 1.0
 * @date 2021/10/7 12:58
 */
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insert(Users users) {
        int i = usersMapper.insert(users);
        if(i != 1){
            System.out.println("【添加用户信息】  添加失败！");
            throw new RuntimeException("【添加用户信息】  添加失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void updateById(Users users) {
        int i = usersMapper.updateByPrimaryKeySelective(users);
        if(i != 1){
            System.out.println("【修改用户信息】 修改失败！");
            throw new RuntimeException("【修改用户信息】  修改失败！");
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void deleteById(Integer id) {
        int i = usersMapper.deleteByPrimaryKey(id);
        if(i != 1){
            System.out.println("【删除用户信息】  删除失败！");
            throw new RuntimeException("【删除用户信息】  删除失败！");
        }
    }

    @Override
    public Users findById(Integer id) {
        return usersMapper.findById(id);
    }

    @Override
    public PageInfo<Users> findByTypeIdAndPage(Integer page, Integer rows, Integer typeId) {
        //1. 分页
        PageHelper.startPage(page,rows);

        //2. 查询
        List<Users> airList = usersMapper.findByTypeId(typeId);

        //3. 封装PageInfo
        PageInfo<Users> pageInfo = new PageInfo<>(airList);

        //4. 返回
        return pageInfo;
    }
}
